package community.hospital.convert;

import cn.hutool.core.convert.impl.BeanConverter;
import cn.hutool.core.lang.func.Func;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import community.hospital.common.R;
import community.hospital.entity.response.PageResponse;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

/**
 * @Author：ydj
 * @Date：2023/1/16 22:54
 */
@Mapper
public abstract class PageConvert {
    public static  PageConvert INSTANCE = Mappers.getMapper(PageConvert.class);
    @Mapping(source = "records",target = "items")
    public abstract  PageResponse converToPageResp(Page page);
    public static <T> Page<T> convert(Page page, List<T> data) {
        Page<T> page1 = new Page<>();
        page1.setTotal(page.getTotal());
        page1.setSize(page.getSize());
        page1.setCurrent(page.getCurrent());
        page1.setPages(page.getPages());
        page1.setRecords(data);
        return page1;
    }

    public static <T, R> Page<R> convert(Page<T> page, Function<T, R> function) {
        Page<R> tPage = new Page<>();
        tPage.setTotal(page.getTotal());
        tPage.setSize(page.getSize());
        tPage.setCurrent(page.getCurrent());
        tPage.setPages(page.getPages());
        List<R> list = page.getRecords().stream().map(function).collect(Collectors.toList());
        tPage.setRecords(list);
        return tPage;
    }

}
